package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.HttpStatus;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.Recharge;
import com.tbit.uqbike.object.pojo.UserRefundLog;
import com.tbit.uqbike.object.pojo.dto.OrderRefundDTO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.RefundService;
import com.tbit.uqbike.service.business.RideLogDetailsService;
import com.tbit.uqbike.service.business.UserRefundLogService;
import com.tbit.uqbike.webmanager.dao.core.RechargeDao;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import com.tbit.utils.NullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 退款日志Controller
 *
 * @author jing
 * @date 2021-05-26
 */
@RestController
@RequestMapping("/refund")
public class UserRefundLogController extends BaseController {
    @Autowired
    private UserRefundLogService userRefundLogService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private RefundService refundService;
    @Autowired
    private RideLogDetailsService rideLogDetailsService;
    @Autowired
    private RechargeDao rechargeDao;

    /**
     * 查询退款日志列表
     */
    @GetMapping("/list")
    public JsonResult list(UserRefundLog userRefundLog) {
        /**获取这个品牌下所有的区域id、区域运营商id*/
        userRefundLog.setAccountIds(accountService.getAllAccountId(userRefundLog.getAccountId()).stream().map(Account::getAccountId).collect(Collectors.toList()));
        PageInfo<UserRefundLog> pageInfo = userRefundLogService.selectUserRefundLogList(userRefundLog);
        return JsonResult.ok(pageInfo.getList(), (int)pageInfo.getTotal(),null);
    }

    /**
     * 导出退款日志列表
     */
    @GetMapping("/export")
    public JsonResult export(UserRefundLog userRefundLog) {
        /**获取这个品牌下所有的区域id、区域运营商id*/
        userRefundLog.setAccountIds(accountService.getAllAccountId(userRefundLog.getAccountId()).stream().map(Account::getAccountId).collect(Collectors.toList()));
        PageInfo<UserRefundLog> pageInfo = userRefundLogService.selectUserRefundLogList(userRefundLog);
        List<UserRefundLog> list = pageInfo.getList();
        for (UserRefundLog refundLog : list) {
            if (!NullUtils.isEmpty(refundLog.getOrderMoney()) && !NullUtils.isEmpty(refundLog.getReturnMoney())) {
                refundLog.setOrderMoney(refundLog.getOrderMoney() * 1 / 100);
                refundLog.setReturnMoney(refundLog.getReturnMoney() * 1 / 100);
            }
        }
        ExcelUtil<UserRefundLog> util = new ExcelUtil<UserRefundLog>(UserRefundLog.class);
        return util.exportExcel(list, "退款日志");
    }

    /**
     * 根据订单编号退款申请 (已弃用，但是要兼容旧版本接口,这里是卡类退款)
     */
    @GetMapping("/apply")
    @OperateLog(module = ModuleEnum.REFUND, methods = MethodEnum.ORDER_REFUND_OPERATION)
    public JsonResult apply(Recharge recharge) throws ParseException {
        Recharge rechargeInDB = rechargeDao.selectByOrderNO(recharge.getOrderNO());
        if (rechargeInDB == null) {
            return JsonResult.fail("订单不存在", TraceUtil.getTraceIdByLocal());
        }
        Result result = refundService.orderRefund(getLogin(ServletUtils.getRequest()), new OrderRefundDTO(rechargeInDB.getUserId(), rechargeInDB.getAccountId(), recharge.getRefundMoney().longValue(), recharge.getOperaPsw(), recharge.getOrderNO()));
        if (result.getCode().equals(HttpStatus.SUCCESS)) {
            return JsonResult.ok();
        } else {
            return JsonResult.fail(result.getMsg(),TraceUtil.getTraceIdByLocal());
        }

        /**操作密码校验*/
        /*if (!OperaPswConstant.DEPOSIT_OPERA_UPDATE_MONEY.equals(recharge.getOperaPsw())) {
            throw new BaseException(SystemCodeEnum.OPERA_PAW_ERROR);
        }
        LoginInfo login = getLogin(ServletUtils.getRequest());
        return userRefundLogService.apply(recharge, login);*/
    }

}

